package com.yicoollxing.common;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

public class TokenCache {
	private static Logger logger = LoggerFactory.getLogger(TokenCache.class);
	public static String TOKEN_PREFIX="token_";

	//LUR算法   最少使用进行清楚
	private static LoadingCache<String, String> localCache = CacheBuilder
			.newBuilder().initialCapacity(1000).maximumSize(10000)
			.expireAfterAccess(12, TimeUnit.HOURS)
			.build(new CacheLoader<String, String>() {
				// 默认数据加载实现，如果key没有对应的实现，就调用这个方法进行加载
				@Override
				public String load(String arg0) throws Exception {
					return "null";
				}
			});

	public static void setKey(String key, String value) {
		localCache.put(key, value);
	}

	public static String getKey(String key) {

		try {
			String vaule = null;
			if ("null".equals(localCache.get(key))) {
				return null;
			}
			vaule=localCache.get(key);
			return vaule;
		} catch (Exception e) {
			logger.error("localCache  get error", e);
			return null;
		}
	}
}
